/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanosn.coffeemaker.application.commands.generate.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author selkhateeb
 */
public class EntityColumn {

    Field field;
    Method getter;
    Method setter;

    public EntityColumn(Field field, Method getter, Method setter) {
        this.field = field;
        this.getter = getter;
        this.setter = setter;
    }

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    public Method getGetter() {
        return getter;
    }

    public void setGetter(Method getter) {
        this.getter = getter;
    }

    public Method getSetter() {
        return setter;
    }

    public void setSetter(Method setter) {
        this.setter = setter;
    }

    public Map<String, Object> getAsMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        fieldMap.put("name", field.getName());
        fieldMap.put("type", field.getType().getSimpleName());

        if (field.getType().getTypeParameters().length == 1) {
            fieldMap.put("hasTypeParameter", true);
            Type genericFieldType = field.getGenericType();
            if (genericFieldType instanceof ParameterizedType) {
                ParameterizedType aType = (ParameterizedType) genericFieldType;
                Type[] fieldArgTypes = aType.getActualTypeArguments();
//                for (Type fieldArgType : fieldArgTypes) {
//                    Class fieldArgClass = (Class) fieldArgType;
//                    System.out.println("fieldArgClass = " + fieldArgClass);
//                }
                Class fieldArgClass = (Class) fieldArgTypes[0];
                fieldMap.put("typeParameter", fieldArgClass.getSimpleName());
            }

        }
        else {
            fieldMap.put("hasTypeParameter", false);
        }
        map.put("field", fieldMap);
        map.put("getter", getter.getName());
        map.put("setter", setter.getName());

        return map;
    }
}
